package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

        @Autowired
        private WmNewsService wmNewsService;
    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    @Override
    public ResponseResult addSensitive(WmChannel wmChannel) {
        //1.校验参数
        if(wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置时间格式为2021-05-23 15:38:51
        wmChannel.setCreatedTime(new java.sql.Timestamp(System.currentTimeMillis()));
        //2.保存频道
        save(wmChannel);
        //3.返回响应
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult deleteSensitive(Integer id) {
        //1.校验参数
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断状态是否是禁用
        WmChannel wmChannel = getById(id);
        if(wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该频道已经被启用");
        }
        //判断该Channel是否有文章
        if(wmNewsService.countByChannelId(id)>0){
            return ResponseResult.errorResult(1002,"该频道下有文章，不能删除");
        }

        //2.删除频道
        removeById(id);
        //3.返回响应
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateSensitive(WmChannel wmChannel) {
        //1.校验参数
        if(wmChannel==null||wmChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置时间格式为2021-05-23 15:38:51
        wmChannel.setCreatedTime(new java.sql.Timestamp(System.currentTimeMillis()));
        //2.修改频道
        updateById(wmChannel);
        //3.返回响应
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult page(WmChannelDto dto) {
        //1.校验参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.分页参数检查
        dto.checkParam();
        //3.查询敏感词
        IPage page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getName() != null && !"".equals(dto.getName())) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }

        queryWrapper.orderByDesc(WmChannel::getOrd);
        page = page(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }
}